package com.bort.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bort.system.mapper.CategoryMapper;
import com.bort.system.model.Category;
import com.bort.system.model.User;
import com.bort.system.service.CategoryService;
import com.bort.system.utils.FileUtils;
import com.bort.system.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    FileUtils fileUtils;

    @Override
    public Result<Page<Category>> getCategoryList(Page<Category> pager, Map<String, Object> map) {
        Result<Page<Category>> result = new Result<>();
        List<Category> list = categoryMapper.getCategoryList(pager,map);
        pager.setRecords(list);
        result.setData(pager);
        return result;
    }

    @Override
    public int addCategory(Category category) {

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        category.setCreateUserId(user.getId());
        category.setCreateTime(new Date());
        boolean res = save(category);
        return res?1:0;
    }

    @Override
    public Result upload(MultipartFile file) {
        return fileUtils.upload(file,"category");
    }

    @Override
    public Result updateCategory(Category category) {
        updateById(category);
        return Result.success();
    }

    @Override
    public Result delete(Integer categoryId) {
        categoryMapper.deleteById(categoryId);
        return Result.success();
    }

    @Override
    public Result<List<Category>> loadAllCategories(Map<String,Object> map) {
        List<Category> list = categoryMapper.getCategoryList(map);
        return Result.success(list);
    }


}
